#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <thread>

#include "logutil.h"
#include <sys/syscall.h>

using namespace tnet;

#define rootlog 	LogUtil::rootLog()
#define HLOG_INIT(logdir,logname,level,logtype) rootlog.open(logdir,logname,level,logtype)
#define HLOG_TRACE(fmt, args...) rootlog.trace(__FILE__, __FUNCTION__, __LINE__, fmt, ##args)
#define HLOG_DEBUG(fmt, args...) rootlog.debug(__FILE__, __FUNCTION__, __LINE__, fmt, ##args)
#define HLOG_INFO(fmt, args...)  rootlog.info(__FILE__,  __FUNCTION__, __LINE__, fmt, ##args)
#define HLOG_WARN(fmt, args...)  rootlog.warn(__FILE__,  __FUNCTION__, __LINE__, fmt, ##args)
#define HLOG_ERROR(fmt, args...) rootlog.error(__FILE__, __FUNCTION__, __LINE__, fmt, ##args)
#define HLOG_FATAL(fmt, args...) rootlog.fatal(__FILE__, __FUNCTION__, __LINE__, fmt, ##args)  


pid_t gettid()
{
    syscall( __NR_gettid );
}

void logutil_process_test()
{
    printf("getpid() = %d,gettid() = %d \n",getpid(),gettid());
    for(int i=0; i<1;i++)
    {
        HLOG_INFO("logutil test start %d",i);
        HLOG_DEBUG("TTEST TEST TEST");
        HLOG_INFO("logutil test end %d",i);
    }
}

void* logutil_thread_test()
{
    printf("logutil_thread_test getpid() = %d,gettid() = %d \n",getpid(),gettid());
    for(int i=0; i<100;i++)
    {
        HLOG_INFO("logutil test %d",i);
    }
    return NULL;
}

int main(int argc,char* argv[0])
{
    rootlog.openlog(argv[0]);

    for(int i=0;i < 1; i++)
    {
        int pid =fork();
        if(pid == 0)
        {
            logutil_process_test();
            exit(0);
        }
    }

    std::thread t(logutil_thread_test);
    t.join();
    return 0;
}